/*
 * ErrorDialog.java
 *
 * Created on September 8, 2005, 10:55 AM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistAddAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistInsertAlbumAction;
import com.bff.slimserver.musicjukebox.lib.actions.PlaylistLoadAlbumAction;
import com.bff.slimserver.musicjukebox.ui.components.CoverArtPanel;
import com.bff.slimserver.musicjukebox.ui.components.MusicJDialog;
import com.bff.bjj.utils.Resources.LABEL_KEY;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.musicobjects.SlimAlbum;

/**
 *
 * @author  Bill
 */
public class AlbumThumbnailDialog extends MusicJDialog {

    private SlimAlbum album;
    private SlimPlaylist playlist;
    private ThumbnailPanel tp;
    private boolean viewingSongs;
    private SongListTablePanel songListTablePanel;

    /** Creates new form ErrorDialog */
    public AlbumThumbnailDialog(Frame frame, SlimAlbum album) {
        super(frame);
        setTitle(getResource(LABEL_KEY.ALBUM_THUMBNAIL_TITLE));
        this.album = album;
        try {
            this.playlist = JukeboxProperties.getInstance().getPlaylist();
        } catch (PlaylistException ex) {
            Logger.getLogger(AlbumThumbnailDialog.class.getName()).log(Level.SEVERE, null, ex);
        }

        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension d = tk.getScreenSize();
        int heightScreen = d.height / 2;
        int widthScreen = d.width / 2;

        tp = new ThumbnailPanel();
        tp.setPreferredSize(new Dimension(500, 500));

        songListTablePanel = new SongListTablePanel(false);

        listSongs();

        initComponents();

        setLocation(widthScreen - (int) (getWidth() / 2), heightScreen - (int) (getHeight() / 2));

        if (JukeboxProperties.getInstance().isShowCoverArt()) {            
            tp.loadImage(album, JukeboxProperties.getInstance().getImageDelayInMillis(), true);
        } else {
            remove(imagePanel);
            add(songListTablePanel);
            btnPanel.remove(btnViewSongs);
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnPanel = new com.bff.bjj.ui.component.BJJPanel();
        btnAddAlbum = new com.bff.bjj.ui.component.BJJButton();
        btnInsertPlaylist = new com.bff.bjj.ui.component.BJJButton();
        btnLoadPlaylist = new com.bff.bjj.ui.component.BJJButton();
        btnViewSongs = new com.bff.bjj.ui.component.BJJButton();
        btnClose = new com.bff.bjj.ui.component.BJJButton();
        btnAddFavorite = new com.bff.bjj.ui.component.BJJButton();
        imagePanel = new CoverArtPanel();
        imagePanel.add(tp);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Album Art");

        btnPanel.setLayout(new java.awt.GridBagLayout());

        btnAddAlbum.setAction(new PlaylistAddAlbumAction(playlist,getAlbum()));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnAddAlbum, gridBagConstraints);

        btnInsertPlaylist.setAction(new PlaylistInsertAlbumAction(getPlaylist(), getAlbum()));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnInsertPlaylist, gridBagConstraints);

        btnLoadPlaylist.setAction(new PlaylistLoadAlbumAction(getPlaylist(), getAlbum()));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnLoadPlaylist, gridBagConstraints);

        btnViewSongs.setText("Songs");
        btnViewSongs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnViewSongsActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnViewSongs, gridBagConstraints);

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnClose, gridBagConstraints);

        btnAddFavorite.setAction(new com.bff.slimserver.musicjukebox.lib.actions.AddFavoriteAction(getAlbum()));
        btnAddFavorite.setText("Add to Favorites");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        btnPanel.add(btnAddFavorite, gridBagConstraints);

        getContentPane().add(btnPanel, java.awt.BorderLayout.SOUTH);

        imagePanel.setBackground(new java.awt.Color(0, 0, 0));
        imagePanel.setPreferredSize(new java.awt.Dimension(750, 500));
        getContentPane().add(imagePanel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

private void btnViewSongsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnViewSongsActionPerformed
    if (viewingSongs) {
        viewingSongs = false;
        remove(songListTablePanel);
        add(imagePanel);
        btnViewSongs.setText(getResource(LABEL_KEY.ALBUM_THUMBNAIL_BUTTON_LABEL_SONGS));
    } else {
        viewingSongs = true;
        remove(imagePanel);
        add(songListTablePanel);
        btnViewSongs.setText(getResource(LABEL_KEY.ALBUM_THUMBNAIL_BUTTON_LABEL_ALBUM));
    }
    repaint();
}//GEN-LAST:event_btnViewSongsActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddAlbum;
    private javax.swing.JButton btnAddFavorite;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnInsertPlaylist;
    private javax.swing.JButton btnLoadPlaylist;
    private javax.swing.JPanel btnPanel;
    private javax.swing.JButton btnViewSongs;
    private javax.swing.JPanel imagePanel;
    // End of variables declaration//GEN-END:variables

    public SlimAlbum getAlbum() {
        return album;
    }

    public void setAlbum(SlimAlbum album) {
        this.album = album;
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    private void listSongs() {
        try {
            getSongListTablePanel().setAlbum(getAlbum());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private SongListTablePanel getSongListTablePanel() {
        return (SongListTablePanel) songListTablePanel;
    }
}
